import br.JZSolution.domineduplex.*

class BootStrap {

    def init = { servletContext ->

        def mapa = [
            [nome:"Jonatas", email:"jonatas@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Daniel", email:"daniel@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Enrique", email:"enrique@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Ash", email:"ash@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Pikachu", email:"pikachu@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Bubasaur", email:"bubasaur@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Sqd", email:"sqd@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Fábio", email:"fabio@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Haroldo", email:"haroldo@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Carlos", email:"carlos@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Clecle", email:"clecle@jzsolution.com", senha:"admin", porta:"6041"],
            [nome:"Teste", email:"teste@jzsolution.com", senha:"admin", porta:"6041"]
        ]

        if(Usuario.list().size() ==0){
            mapa.each{ usu ->
                try{
                    new Usuario(usu).save(flush:true, failOnError:true)
                }catch (Exception e){
                    e.printStackTrace()
                }
            }
        }

        def usu1 = Usuario.findByEmail(mapa.get(1).email)
        println "usu1 = "+usu1

        usu1.listaDeContatos = []
        usu1.listaDeContatos << Usuario.findByEmail(mapa.get(2).email)
        usu1.listaDeContatos << Usuario.findByEmail(mapa.get(3).email)
        usu1.listaDeContatos << Usuario.findByEmail(mapa.get(4).email)

        def usu2 = Usuario.findByEmail(mapa.get(5).email)
        println "usu2 = "+usu2

        usu2.listaDeContatos = []
        usu2.listaDeContatos << Usuario.findByEmail(mapa.get(6).email)
        usu2.listaDeContatos << Usuario.findByEmail(mapa.get(7).email)
        usu2.listaDeContatos << Usuario.findByEmail(mapa.get(8).email)

    }

    def destroy = {
        
    }

}
